package com.hctg.forced2sleep.adapter;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hctg.forced2sleep.R;
import com.hjq.shape.view.ShapeTextView;

import org.jetbrains.annotations.NotNull;

/**
 * description: 描述
 * company    :
 *
 * @author : ldf
 * date       : 2024/12/1 on 13
 * @version 1.0
 */
public class SleepTimeSetDialogAdapter extends BaseQuickAdapter<Integer, BaseViewHolder> {

    private int selectedItem = 0;

    public SleepTimeSetDialogAdapter() {
        super(R.layout.item_sleep_time_set_dialog);
        setOnItemClickListener((adapter, view, position) -> {
            if (position != selectedItem) {
                int oldPos = selectedItem;
                selectedItem = position;
                notifyItemChanged(oldPos);
                notifyItemChanged(position);
            }
        });
    }

    @Override
    protected void convert(@NotNull BaseViewHolder holder, Integer item) {
        int position = holder.getAbsoluteAdapterPosition();
//        holder.setText(R.id.tv_minute, String.format("%02d分钟", item));
        ShapeTextView stv = holder.getView(R.id.tv_minute);
        stv.setSelected(position == selectedItem);
        stv.setText(String.format("%02d分钟", item));
    }

    //获取选中的分钟数
    public int getSelectedMinute() {
        Integer item = getItemOrNull(selectedItem);
        return item == null ? getItem(0) : item;
    }
}
